package com.spark.demoim.ui

import android.view.View
import android.widget.*
import com.spark.demoim.global.App
import org.jetbrains.anko.AnkoComponent
import org.jetbrains.anko.AnkoContext

/**
 * Created by lenovo on 2017/8/17.
 */
open class LoginLayout(val logRegisterView: LogRegisterView) : AnkoComponent<LoginActivity> {
    override fun createView(ui: AnkoContext<LoginActivity>): View {
        return View(App.appContext)
    }

    var loginTilebar: LinearLayout? = null
    var welcomeText: TextView? = null
    var resisterOrLoginLayout: RelativeLayout? = null
    var loginTextHint: TextView? = null
    var regionNum: TextView? = null
    var phoneNum: TextView? = null
    var btnRegister: Button? = null
    var tvContract: TextView? = null
    var phoneLayout: RelativeLayout? = null
    var ivClose: ImageView? = null
    var tvLoginPhone: TextView? = null
    var tvLoginEmail: TextView? = null
    var isRegisterState = true
    var userPhoneLogin = true
/*    override fun createView(ui: AnkoContext<LoginActivity>): View = with(ui) {
        val rootView = relativeLayout {
            lparams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
            */
    /**
     * 标题
     */
    /*
                loginTilebar = linearLayout {
                    backgroundColor = resources.getColor(R.color.colorPrimary)
                    ivClose = imageView(R.drawable.close) {
                        padding = dip(10)
                    }.lparams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                }.lparams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                */
    /**
     * 欢迎文字
     */
    /*
                welcomeText = textView {
                    if (isRegisterState) {
                        text = "欢迎来到知乎"
                    } else {
                        text = "登陆知乎"
                    }
                    backgroundColor = resources.getColor(R.color.colorPrimary)
                    textSize = sp(12).toFloat()
                    textColor = Color.WHITE
                    setPadding(10, 0, 10, 0)
                }.lparams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
                    //padding = dip(10)
                    bottomMargin = dip(10)
                    below(loginTilebar)
                }
                */
    /**
     * 登陆注册状态布局切换
     */
    /*
                resisterOrLoginLayout = relativeLayout {
                    textView("注册") {
                        textSize = sp(11).toFloat()
                        textColor = Color.GRAY

                        fun changVisible() {
                            if (isRegisterState) {
                                visibility = View.VISIBLE
                            } else {
                                visibility = View.GONE
                            }
                        }

                    }.lparams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
                        alignParentLeft()
                        centerVertically()
                    }

                    relativeLayout {
                        tvLoginPhone = textView("手机号") {
                            if (!isRegisterState && userPhoneLogin) {
                                textColor = resources.getColor(R.color.colorPrimary)
                                val bottom_drawable = resources.getDrawable(R.drawable.login_switch_item_bottom_blue_drawable)
                                bottom_drawable.setBounds(0, 0, bottom_drawable.intrinsicWidth, bottom_drawable.intrinsicHeight)
                                setCompoundDrawables(null, null, null, bottom_drawable)
                            } else {
                                textColor = Color.WHITE
                                val bottom_drawable = resources.getDrawable(R.drawable.login_switch_item_bottom_white_drawable)
                                bottom_drawable.setBounds(0, 0, bottom_drawable.intrinsicWidth, bottom_drawable.intrinsicHeight)
                                setCompoundDrawables(null, null, null, bottom_drawable)
                            }
                        }.lparams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
                            alignParentLeft()
                            centerVertically()
                        }

                        tvLoginEmail = textView("邮箱") {
                            if (!isRegisterState && userPhoneLogin) {
                                textColor = resources.getColor(R.color.colorPrimary)
                                val bottom_drawable = resources.getDrawable(R.drawable.login_switch_item_bottom_blue_drawable)
                                bottom_drawable.setBounds(0, 0, bottom_drawable.intrinsicWidth, bottom_drawable.intrinsicHeight)
                                setCompoundDrawables(null, null, null, bottom_drawable)
                            } else {
                                textColor = Color.WHITE
                                val bottom_drawable = resources.getDrawable(R.drawable.login_switch_item_bottom_white_drawable)
                                bottom_drawable.setBounds(0, 0, bottom_drawable.intrinsicWidth, bottom_drawable.intrinsicHeight)
                                setCompoundDrawables(null, null, null, bottom_drawable)
                            }
                        }.lparams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
                            alignParentLeft()
                            centerVertically()
                        }

                        if (isRegisterState) {
                            visibility = View.GONE
                        } else {
                            visibility = View.VISIBLE
                        }
                    }.lparams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
                        alignParentRight()
                        centerVertically()
                        gravity = Gravity.CENTER
                    }
                    */
    /**
     * 切换开关
     */
    /*
                    textView {
                        if (isRegisterState) {
                            text = "有账号？去登陆"
                        } else {
                            text = "去注册"
                        }
                        textColor = resources.getColor(R.color.colorPrimary)
                        val right_arrow = resources.getDrawable(R.drawable.right_arrow)
                        right_arrow.setBounds(0, 0, right_arrow.intrinsicWidth, right_arrow.intrinsicHeight)
                        setCompoundDrawables(null, null, right_arrow, null)
                    }.lparams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
                        alignParentRight()
                        centerVertically()
                        gravity = Gravity.CENTER
                    }.setOnClickListener {
                        // 更改界面，切换登陆注册状态
                        isRegisterState = !isRegisterState
                        //logRegisterView.phoneIsNull()
                        //rootView.invalidate()
                    }
                }.lparams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
                    margin = dip(10)
                    below(welcomeText)

                }

                phoneLayout = relativeLayout {
                    regionNum = textView("CN +86") {
                        textSize = sp(10).toFloat()
                        val right_arrow = resources.getDrawable(R.drawable.right_arrow)
                        right_arrow.setBounds(0, 0, right_arrow.intrinsicWidth, right_arrow.intrinsicHeight)
                        setCompoundDrawables(null, null, right_arrow, null)
                    }.lparams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
                        margin = dip(10)
                        below(resisterOrLoginLayout)
                        centerVertically()
                    }

                    editText {
                        id = phoneNum
                        hint = "请输入手机号"
                        background = null
                    }.lparams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
                        rightOf(regionNum)
                        centerVertically()
                    }

                }.lparams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
                    below(resisterOrLoginLayout)
                }

                imageView() {
                    backgroundColor = Color.BLACK
                }.lparams(ViewGroup.LayoutParams.MATCH_PARENT, dip(1)) {
                    below(phoneLayout)
                    margin = dip(10)
                }
                */
    /**
     * 注册按钮
     */
    /*
                  button("注册") {
                    backgroundColor = Color.GRAY
                }.lparams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
                    alignParentBottom()
                    gravity = Gravity.CENTER
                }.setOnClickListener {
                    val checkPhoneResult = checkPhone(find<EditText>(phoneNum).text.toString().trim())
                    if (checkPhoneResult) {

                    }
                }
                */
    /**
     * 协议
     */
    /*
                tvContract = textView {
                    if (isRegisterState) {
                        text = "选择\n[注册]\n代表您已阅读并同意\n知乎协议"
                    } else {
                        text = "选择社交登陆"
                    }
                    gravity = Gravity.CENTER_HORIZONTAL
                }.lparams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
                    above(btnRegister)
                    margin = dip(10)
                    centerHorizontally()
                }
            }
            rootView
        }*/

    /**
     * 校验手机号
     */
    fun checkPhone(phone: String): Boolean {
        if (phone.isEmpty()) {
            logRegisterView.phoneIsNull()
            return false
        } else {
            val regex = "[1][34578]\\d{9}"
            val isMatched = phone.matches(Regex(regex))
            if (isMatched)
                return true
            else {
                logRegisterView.phoneIsError()
                return false
            }
        }
    }
}